package com.bingxue.edu.management.shop.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.excel.util.ExcelUtil;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.shop.model.query.PayRecordQuery;
import com.bingxue.edu.management.shop.model.query.RefundRecordQuery;
import com.bingxue.edu.management.shop.model.resp.PayRecordExportResp;
import com.bingxue.edu.management.shop.model.resp.PayRecordListResp;
import com.bingxue.edu.management.shop.model.resp.PayRecordSummaryResp;
import com.bingxue.edu.management.shop.model.resp.RefundRecordExportResp;
import com.bingxue.edu.management.shop.model.resp.RefundRecordListResp;
import com.bingxue.edu.management.shop.model.resp.RefundRecordSummaryResp;
import com.bingxue.edu.management.shop.service.PayRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author panbo
 * @since 2024/11/28
 */
@Slf4j
@Tag(name = "收支记录接口")
@RestController
@RequestMapping("/shop")
@RequiredArgsConstructor
public class PayRecordController {

    private final PayRecordService payRecordService;

    @Operation(summary = "分页查询收入记录")
    @ApiResource("shop:pay-record:read")
    @GetMapping("/pay/record/page")
    public Response<Pagination<PayRecordListResp>> getPayRecordPage(PayRecordQuery query, @Validated PageQuery pageQuery) {
        return Response.success(Pagination.of(payRecordService.getPayRecordPage(query, pageQuery)));
    }

    @Operation(summary = "导出收入记录")
    @ApiResource("shop:pay-record:read")
    @GetMapping(path = "/pay/record/export")
    public void exportPayRecord(PayRecordQuery query, HttpServletResponse response) {
        List<PayRecordExportResp> records = payRecordService.getPayRecordList(query).stream().map(PayRecordExportResp::of).toList();
        ExcelUtil.export(records, "收款明细", PayRecordExportResp.class, response);
    }

    @Operation(summary = "查询收入记录总览")
    @ApiResource("shop:pay-record:read")
    @GetMapping("/pay/record/summary")
    public Response<PayRecordSummaryResp> getPayRecordSummary(PayRecordQuery query) {
        return Response.success(payRecordService.getPayRecordSummary(query));
    }

    @Operation(summary = "分页查询退款记录")
    @ApiResource("shop:pay-record:read")
    @GetMapping("/refund/record/page")
    public Response<Pagination<RefundRecordListResp>> getRefundRecordPage(RefundRecordQuery query, @Validated PageQuery pageQuery) {
        return Response.success(Pagination.of(payRecordService.getRefundRecordPage(query, pageQuery)));
    }

    @Operation(summary = "导出退款记录")
    @ApiResource("shop:pay-record:read")
    @GetMapping(path = "/refund/record/export")
    public void exportRefundRecord(RefundRecordQuery query, HttpServletResponse response) {
        List<RefundRecordExportResp> records = payRecordService.getRefundRecordList(query).stream().map(RefundRecordExportResp::of).toList();
        ExcelUtil.export(records, "退款明细", RefundRecordExportResp.class, response);
    }

    @Operation(summary = "查询退费记录总览")
    @ApiResource("shop:pay-record:read")
    @GetMapping("/refund/record/summary")
    public Response<RefundRecordSummaryResp> getRefundRecordSummary(RefundRecordQuery query) {
        return Response.success(payRecordService.getRefundRecordSummary(query));
    }
}
